package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.CatalogPropertyValue;
import com.xmg.shop.api.service.ICatalogPropertyService;
import com.xmg.shop.api.service.ICatalogPropertyValueService;
import com.xmg.shop.api.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CatalogPropertyValueController {
    @Reference
    private ICatalogPropertyService catalogPropertyService;
    @Reference
    private ICatalogPropertyValueService catalogPropertyValueService;

    @RequestMapping("/catalogPropertyValue/get/{propertyId}")
    public String getPropertyValue(@PathVariable("propertyId") Long propertyId, Model model) {
        model.addAttribute("catalogProperty", catalogPropertyService.get(propertyId));
        List<CatalogPropertyValue> list = catalogPropertyValueService.selectByPropertyId(propertyId);
        model.addAttribute("list", list);
        return "property/property_value_list";
    }

    @RequestMapping("/catalogPropertyValue/add")
    @ResponseBody
    public AjaxResult addPropertyValue(Long catalogPropertyId,String[] value ) {
        AjaxResult result = null;
        try {
            CatalogPropertyValue catalogPropertyValue ;
            for (String v : value) {
                catalogPropertyValue = new CatalogPropertyValue();
                catalogPropertyValue.setCatalogPropertyId(catalogPropertyId);
                catalogPropertyValue.setValue(v);
                catalogPropertyValueService.save(catalogPropertyValue);
            }
            result = new AjaxResult(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/catalogPropertyValue/delete/{id}")
    @ResponseBody
    public AjaxResult deleteValue(@PathVariable("id") Long id) {
        AjaxResult result = null;
        try {
            catalogPropertyValueService.delete(id);
            result = new AjaxResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new AjaxResult(e.getMessage());
        }
        return result;
    }
}
